package com.ailiwu.www.ailiwu.activity;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.ailiwu.www.ailiwu.Fragment.FragmentSearchMain;
import com.ailiwu.www.ailiwu.R;
import com.ailiwu.www.ailiwu.base.BaseActivity;
import com.ailiwu.www.ailiwu.bean.HotSearchBean;
import com.ailiwu.www.ailiwu.url.Urls;
import com.ailiwu.www.ailiwu.utils.Logger;
import com.google.gson.Gson;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.ArrayList;
import java.util.List;

public class SearchActivity extends BaseActivity {
    private List<String> hotwords = new ArrayList<>();
    private MyAdapter adapter;
    private String key;
    private TextView tvSearch;


    @Override
    protected void loadXml() {
        setContentView(R.layout.activity_search);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    protected void getIntentData(Bundle savedInstanceState) {

    }

    @Override
    protected void initView() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        final EditText editText = (EditText) findViewById(R.id.editText);
        editText.requestFocus();

        editText.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == 66) {
                    String string = editText.getText().toString();
                    searchByKey(string);
                }
                return false;
            }
        });
        tvSearch = (TextView) findViewById(R.id.btn_search);


        //点解搜索按钮是调用FragmentMain现实搜索结果；
        tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String string = editText.getText().toString();
                searchByKey(string);
            }
        });

        RecyclerView mRyRecyclerView = (RecyclerView) findViewById(R.id.recyclerView_search);
        mRyRecyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        adapter = new MyAdapter();
        mRyRecyclerView.setAdapter(adapter);
    }

    //通过哟
    private void searchByKey(String string) {
        key = string;
        if (TextUtils.isEmpty(key)) {
            Snackbar.make(tvSearch, "搜索内容不能为空", Snackbar.LENGTH_SHORT).show();
        } else {
            FragmentManager supportFragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
            Fragment fragment = FragmentSearchMain.newInstance(string);
            fragmentTransaction.replace(R.id.fl_content, fragment);
            fragmentTransaction.commit();
    }
    }

    class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyViewHolder(View.inflate(SearchActivity.this, R.layout.item_search_recycler, null));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            ((MyViewHolder) holder).tv.setText(hotwords.get(position));
            ((MyViewHolder) holder).view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String s = hotwords.get(position);
                    Logger.e(s);
                    searchByKey(s);
                }
            });
        }

        @Override
        public int getItemCount() {
            return hotwords.size();
        }

        public class MyViewHolder extends RecyclerView.ViewHolder {
            TextView tv;
            View view;

            public MyViewHolder(View itemView) {
                super(itemView);
                this.view = itemView;
                tv = (TextView) itemView.findViewById(R.id.textView);
            }
        }
    }


    private void search() {
        httpUtils.send(HttpRequest.HttpMethod.GET, Urls.HOT_WORD, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String s = responseInfo.result;
                if (s != null) {
                    HotSearchBean hotSearchBean = new Gson().fromJson(s, HotSearchBean.class);
                    List<String> hot_words = hotSearchBean.getData().getHot_words();
                    hotwords = hot_words;
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    @Override
    protected void setListener() {
    }

    @Override
    protected void initData() {
        search();
    }

    private void search(String str) {
        httpUtils.send(HttpRequest.HttpMethod.GET, Urls.SEARCH(str), new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String s = responseInfo.result;
                if (s != null) {
                    HotSearchBean hotSearchBean = new Gson().fromJson(s, HotSearchBean.class);
                    List<String> hot_words = hotSearchBean.getData().getHot_words();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
            case android.R.id.home:
                finish();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}
